% This m-file is to produce the Qz-dependence 1D fluorescing data

MATfile={%_raw.mat data
   'sph01_scan329_raw.mat';      %  bar 3  SPM/KCl/FeCl3 pH 4
   'sph02_scan406_raw.mat';      % bar 3 KCl 100 mM, FeCl3 5 mM
    }

Floures_Fe_Qz_file={ % save _EL (emissionline data as Qz function
    'sph01_scan329_Fe_EL.txt';     % 1   bar 3  SPM/KCl/FeCl3 pH 4
    'sph02_scan406_Fe_EL.txt';     %     bar 3 KCl 100 mM, FeCl3 5 mM
    
    }


Foures_DB_Qz_file={% save _DB (direct beam intensity) data as Qz function
    'sph01_scan329_DB_EL.txt';     % bar 3  SPM/KCl/FeCl3 pH 4
    'sph02_scan406_DB_EL.txt';     % bar 3 KCl 100 mM, FeCl3 5 mM
    }


ch=[1:2048];
CalibCoef=[0.0089,-0.0048];  % Channel->Energy calibration coefficients APS detector
E=ch*CalibCoef(1)+CalibCoef(2);

Target_EL={'Fe','DB'} % Target to-be-integrated emission lines
CaseNum=2;
switch Target_EL{CaseNum}
    case 'Fe'
        Emin=6.25;
        Emax=6.65;
    case 'DB'
        Emin=12.5;
        Emax=14;
    otherwise
        mywarndlg = warndlg('Invalid input', 'My Warn Dialog');
end

EM=1;
EN=2048;
for k=1:length(E)
    if E(k)>Emin;EM=k-1;break;
    end
end

for k=1:length(E)
    if E(k)>Emax;EN=k-1;break;
    end
end
%%
close all
figure
subplot(1,3,1)
hold on
MarkerType={'o','s','v','^','d','>','<'}
COLOR={'k','r','b',[0 0.4 0],'m','k','k'}
for k=1:length(MATfile)
    data=load(MATfile{k});
    temp=sum(data.mca(EM:EN,:),1);
    temp_err=sqrt(temp);
    mon=data.mon;
    qz=data.qz;
    ct=data.ct;
    Intensity1D=temp./mon./ct;
    Intensity1D_err=temp_err./mon./ct;
    H=ploterr(qz,Intensity1D,[],Intensity1D_err,MarkerType{k},'hhy',0.1);
    set(H,'LineWidth',3,'color',COLOR{k});
    switch Target_EL{CaseNum}
        case 'Fe'
            Txt_Save(Floures_Fe_Qz_file{k},qz,Intensity1D,Intensity1D_err);
        case 'DB'
            Txt_Save(Foures_DB_Qz_file{k},qz,Intensity1D,Intensity1D_err);
        otherwise
            mywarndlg = warndlg('Invalid input', 'My Warn Dialog');
    end
end
hold off
set(gca,'LineWidth',3,'box','on');



        
        
        
        
        